<template>
  <div class="add-form">
    <el-dialog :visible="dialogFormVisible"
               title="新增"
               @close="onClose">
      <el-form
        ref="dataForm"
        :model="form"
        :rules="rules"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left:120px;"
      >

        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type !== '1'"
          label="密码"
          prop="password"
        >
          <el-input v-model="form.password"></el-input>
        </el-form-item>

        <!-- 角色 -->
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role"></el-input>
        </el-form-item>
        <!-- 权限组 -->
        <el-form-item label="权限组名称" prop="permission_group_id">
          <el-select v-model="form.permission_group_id" class="filter-item">
            <el-option
              v-for="item in PermissionGroupsList"
              :key="item.key"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input
            v-model="form.introduction"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="Please input"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="onSave">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '../../api/base/permissions'
import { add, update } from '../../api/base/users'

export default {
  name: 'user-add1',
  props: {
    dialogFormVisible: {
      type: Boolean,
      required: true
    },
    type: {
      type: String
    },
    form: {
      type: Object
    }
  },
  data () {
    const phone = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/
    const email = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    return {
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        email: [
          { pattern: email, message: '邮箱格式不正确', trigger: 'blur' },
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        role: [{ required: true, message: '请输入角色', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: phone, message: '目前只支持中国大陆的手机号码', trigger: 'blur' }
        ]
      },
      PermissionGroupsList: ''
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    async onSave () {
      try {
        await this.$refs.dataForm.validate()
        if (this.type !== '1') {
          await add(this.form)
          this.$message.success('添加成功')
        } else {
          await update(this.form)
          this.$message.success('修改成功')
        }
        this.$emit('updateList')
        this.$emit('update:dialogFormVisible', false)
      } catch (err) {
        console.log(err)
      }
    },
    async getPermissionList () {
      const res = await simple()
      this.PermissionGroupsList = res.data
    },
    onClose () {
      this.$refs.dataForm.resetFields()
      this.$emit('update:dialogFormVisible', false)
      const formData = {
        username: '',
        role: '',
        phone: '',
        permission_group_id: '',
        avatar: '',
        email: '',
        password: '',
        introduction: ''
      }
      this.$emit('replacement', formData)
    }
  }
}
</script>

<style lang="less" scoped>
.el-form--label-left .el-form-item__label {
  text-align: right;
}

.el-form-item--medium {
  margin-bottom: 22px;
}

.el-dialog__footer {
  text-align: center
}
</style>
